<?php

namespace Modules\Edu\Http\Controllers\Front;

use App\Services\PayService;
use Browser;
use Endroid\QrCode\QrCode;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Edu\Entities\Order;
use Modules\Edu\Entities\Subscribe;

class SubscribeController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');

    }

    public function index()
    {
        $subscribes = Subscribe::all();

        return view('edu::subscribe_front.index', compact('subscribes'));
    }

    public function order(Subscribe $subscribe)
    {

        $order['sn'] = 'S' . site()['id'] . '-U' . user('id') . '-' . date('YmdHis') . mt_rand(0, 99999);
        $order['site_id'] = site()['id'];
        $order['user_id'] = user('id');
        $order['price'] = $subscribe['price'];
        $order['subject'] = $subscribe['title'];
        $order['type'] = 'subject';
        $order['month'] = $subscribe['month'];
        return Order::create($order);


    }

    public function alipay(Subscribe $subscribe, PayService $payService)
    {

        $order = $this->order($subscribe)->toArray();
        $payService->alipay($order, Browser::isDesktop() ? 'web' : 'wap');
    }

    public function wepay(Subscribe $subscribe, PayService $payService, $action = 'scan')
    {
//        if (!Auth::check()) {
//            if()
//        }

        $order = $this->order($subscribe)->toArray();

        if (Browser::isDesktop()) {

            $payService->wepay($order, 'scan');
        } else
            return $payService->wepay($order, 'wap');
    }


}
